package com.yawn.zddefect.controller;

import com.yawn.zddefect.service.GraphService;
import com.yawn.zddefect.util.WebUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/graph")
public class GraphController {

    @Resource
    private GraphService graphService;

    @GetMapping("/process/{id}")
    public ResponseEntity<byte[]> process(@PathVariable String id) throws IOException {
        InputStream inputStream = graphService.process(id);
        return WebUtil.download(inputStream, "graph");
    }

    @GetMapping("/task/{id}")
    public ResponseEntity<byte[]> task(@PathVariable String id) throws IOException {
        InputStream inputStream = graphService.task(id);
        return WebUtil.download(inputStream, "graph");
    }

    @GetMapping("/graph")
    public ResponseEntity<byte[]> graph() throws IOException {
        InputStream inputStream = graphService.graph();
        return WebUtil.download(inputStream, "graph");
    }
}
